<template>
  <div class="header">
    <div class="info">
      <div class="company">
        <img src="./icon/zhongzi.png" alt="" /> &nbsp; 中咨集团科学与数字化管理
      </div>
    </div>
    <div class="common-layout menu">
      <div class="head-left">
        <div
          v-for="(item, index) in store.state.router.routes"
          :key="item.path"
          class="menu-item"
          @click="switchTab(item.path)"
          :class="{ active: activePath.startsWith(item.path) }"
        >
          <!-- <img :src="item.meta.icon?require('./icon/' + item.meta.icon):require('./icon/console.png')" alt="" /> -->
          <img :src="item.meta.icon ? JSON.parse(item.meta.icon).url : ''" alt="" />
          {{ item.meta.title }}
        </div>
      </div>
      <div class="head-right">
        {{ store.state.system.userInfo?.cname }}
        <img @click="loginOut" src="./icon/tuichu.png" alt="" />
      </div>
    </div>
  </div>
  <router-view></router-view>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus'
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { servicesLoading } from '@/utils/loading'

const router = useRouter()
const route = useRoute()
const store = useStore()
let activePath = ref('/')

store.state.system.userInfo = JSON.parse(localStorage.getItem('userInfo') as any)
// setTimeout(() => {
// }, 100);

const switchTab = (path: string) => {
  const loading = servicesLoading({ fullscreen: false, text: '正在加载路由菜单' })

  router.push(path)
  loading.close()
  store.state.router.currentMenu = path
  activePath.value = path
}
const loginOut = () => {
  ElMessageBox.confirm('确认退出当前登录吗？', '提示', {
    type: 'warning',
    callback: (action) => {
      if (action == 'confirm') {
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        localStorage.removeItem('permissions')
        localStorage.removeItem('routes')
        localStorage.removeItem('zhongziConfig')
        localStorage.clear()
        store.state.router.routes = []
        store.state.projectCenter.orgList = []
        router.push('/login')
      }
    },
  })
}

store.state.router.currentMenu = route.path
activePath.value = route.path

// 监听路由，匹配菜单
watch(
  () => route.path,
  () => {
    activePath.value = route.path
    store.state.router.currentMenu = route.path
  },
  {
    immediate: true,
  }
)
</script>

<style scoped lang="scss">
.header {
  height: 60px;
  // height: 55px;
  display: flex;
  color: aliceblue;
  .info {
    width: 300px;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    line-height: 60px;
    // line-height: 55px;
    padding-left: 5px;
    background-color: #0873dd;
    .company {
      white-space: nowrap;
      text-overflow: ellipsis;
      display: flex;
      align-items: center;
      img {
        width: 30px;
        height: 30px;
      }
    }
  }
  .menu {
    flex: 1;
  }
}
.common-layout {
  display: flex;
  padding: 0 10px;
  background-color: #0873dd;

  .head-left {
    display: flex;
    width: 90%;
    // background-color: #b55353;
    height: 60px;
    align-items: center;
    .menu-item {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      > img {
        width: 20px;
        height: 20px;
        margin-right: 5px;
      }

      &:hover {
        cursor: pointer;
      }

      display: flex;
      align-items: center;
      // padding: 5px 10px;
      padding: 0px 10px;
    }
  }

  .head-right {
    color: #e2e2e2;
    display: flex;
    flex: 1;
    white-space: nowrap;
    text-overflow: ellipsis;
    justify-content: end;
    align-items: center;

    > img {
      width: 20px;
      height: 20px;
      margin: 0 15px;

      &:hover {
        cursor: pointer;
      }
    }
  }
}

.active {
  color: #0873dd;
  line-height: 40px;
  border-radius: 5px;
  // background-color: #e2e2e2;
  background-image: linear-gradient(to bottom, #a9ccee, #d1dff4);
}
</style>
